package com.mall.group_booking.mq;

import com.alibaba.fastjson.JSONObject;
import com.mall.constant.enums.OrderStatus;
import com.mall.constant.strings.ConstantStrings;
import com.mall.constant.strings.MapMsg;
import com.mall.entity.bargain.BgOrder;
import com.mall.entity.groupbooking.GbRoot;
import com.mall.group_booking.entity.GbItemVo;
import com.mall.group_booking.entity.GbRecordVo;
import com.mall.group_booking.feign.OrderFeign;
import com.mall.group_booking.service.BookingRootService;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * 消费
 *
 * @author ASUS
 * @date 2021/07/18
 */
@Component
public class Consume {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private Redisson redisson;

    @Autowired
    private BookingRootService bookingRootService;

    @Autowired
    private OrderFeign orderFeign;


    @JmsListener(destination = "doBookingRoot")
    public void doBookingRoot(String message){
        //解析字符串
        HashMap<Object, Object> hashMap = JSONObject.parseObject(message, HashMap.class);
        GbItemVo gbItemVo= JSONObject.parseObject(hashMap.get("gbItemVo").toString(), GbItemVo.class);
        GbRecordVo gbRecordVo= JSONObject.parseObject(hashMap.get("gbRecordVo").toString(), GbRecordVo.class);
        //将团信息存储到数据库并返回
        GbRoot gbRoot = bookingRootService.doBookingRoot(gbItemVo, gbRecordVo.getUid());
        //获取redis中的团对象
        RMap<Integer, GbRoot> bookingRootMap = redisson.getMap(MapMsg.BOOKING_ROOT_REDIS_MAP);
        RLock lock = bookingRootMap.getLock(gbRoot.getId());
        try {
            lock.lock(5,TimeUnit.SECONDS);
            //将创建的团对象放到redis中
            bookingRootMap.put(gbRoot.getId(),gbRoot);
        }finally {
            lock.unlock();
        }
        //创建订单实例
        BgOrder order = new BgOrder();
        order.setCreateTime(new Date());
        order.setPrice(gbItemVo.getGbPrice());
        //类型为拼团团长单
        order.setType("4");
        order.setItemSpecId(gbItemVo.getSpecItemId());
        order.setUid(gbRecordVo.getUid());
        //状态为未支付
        order.setStatus(OrderStatus.NON_PAYMENT.getCode());
        order.setOrderItemId(Long.parseLong(gbRoot.getId().toString()));
        //远程调用创建订单服务
        order = orderFeign.createOrder(order);
        String ifOrderId= ConstantStrings.IF_BOOKING_ROOT_ORDER_ID+order.getId();
        //将订单超时标志字符存入redis中
        redisTemplate.opsForValue().set(ifOrderId,"",180, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set(ConstantStrings.IF_BOOKING_ROOT_ID+gbRoot.getId(),"",gbRoot.getTime(), TimeUnit.HOURS);
    }

    @JmsListener(destination = "doBookingRecord")
    public void doBookingRecord(String message){
        //解析字符串
        HashMap<Object, Object> hashMap = JSONObject.parseObject(message, HashMap.class);
        GbItemVo gbItemVo= JSONObject.parseObject(hashMap.get("gbItemVo").toString(), GbItemVo.class);
        GbRecordVo gbRecordVo= JSONObject.parseObject(hashMap.get("gbRecordVo").toString(), GbRecordVo.class);
        //创建订单实例
        BgOrder order = new BgOrder();
        order.setCreateTime(new Date());
        order.setPrice(gbItemVo.getGbPrice());
        //设置类型为拼团普通单
        order.setType("5");
        order.setItemSpecId(gbItemVo.getSpecItemId());
        order.setUid(gbRecordVo.getUid());
        //状态为未支付
        order.setStatus(OrderStatus.NON_PAYMENT.getCode());
        order.setOrderItemId(gbRecordVo.getId());
        //远程调用创建订单服务
        order = orderFeign.createOrder(order);
        String ifOrderId= ConstantStrings.IF_BOOKING_RECORD_ORDER_ID+order.getId();
        //将订单超时标志字符存入redis中
        redisTemplate.opsForValue().set(ifOrderId,"",60, TimeUnit.SECONDS);
    }

    @JmsListener(destination = "doBookingRootInvalid")
    public void doBookingRootInvalid(String message){
        GbRoot gbRoot = JSONObject.parseObject(message, GbRoot.class);
        bookingRootService.updateStatusById(gbRoot.getId(),gbRoot.getStatus());
    }

}
